<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\common\model;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\Config;
use app\admin\service\User;
use app\api\model\Token;
use think\Exception;
use think\facade\Db;
use think\Model;
use EasyWeChat\Factory;


class MemberModel extends Model
{
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    protected $table = 'yzn_member';

    public function ali()
    {
        return $this->belongsTo(AliPay::class, 'id', 'uid')->setEagerlyType(0);
    }

    public function getUserInfo($params = array())
    {
        $user = self::where(['id' => $params['uid']])->field('id,nickName,avatar,total_count,is_auth')->find();
        //查询用户最新收益
        //获取最新已经设置单价的日期
        $date = (new RebateLog())->where('money', '>', 0)
            ->where(['uid' => $params['uid']])
            ->order('day desc')
            ->field('day')->find();
        $dayMoney = 0;
       if($date){
           $dayMoney = (new RebateLog())->where(['uid' => $params['uid'],'day' => $date['day']])->sum('money');
       }
        $user['day_money'] = number_format($dayMoney, 2);
        $user['date'] = !isset($date['day'])?' ':$date['day'];
        $config = (new Config())->where(['id' => 15])->value('value');

        return ['code' => 200, 'msg' => '获取成功', 'data' => ['user' => $user, 'prompt' => $config]];

    }


    public function saveUserAuth($params)
    {
        try {
            if (!$params['dy_code'] || !$params['real_name'] || !$params['identity_code']) {
                throw new Exception('缺少参数');
            }

            $user = self::where(['id' => $params['uid']])->find();
            if (!$user) {
                throw new Exception('用户不存在');
            }
            $params['is_auth'] = 1;
            if (!$user->allowField(true)->save($params)) {
                throw new Exception('提交失败，请重新尝试');
            }

            return ['code' => 200, 'msg' => '提交成功，等待审核'];

        } catch (\Exception $e) {
            return ['code' => 201, 'msg' => $e->getMessage()];
        }
    }


    public function getUserAuth($params)
    {
        $user = self::where(['id' => $params['uid']])->field('id,real_name,is_auth,reason,dy_code,identity_code')->find();
        return ['code' => 200, 'msg' => '获取成功', 'data' => $user];
    }


    public function loginOut($params)
    {
        $token = \think\Db::name('user_token')->where(['user_id' => $params['uid']])->delete();
        if ($token) {
            return ['code' => 200, 'msg' => '退出成功', 'data' => []];
        }
        return ['code' => 201, 'msg' => '退出失败'];
    }

}
